import { FormOption } from "@/components/SuperForm/formType";
import { type FormRules } from "element-plus";
import { getMaterialType, getMaterialUnit } from "@/api/materialFile";

type Item = {
  id: number;
  name: string;
};
// 描述内容
export class FormMaterial {
  // 定义变量
  #formData: any = {
    name: "",
    materialUnitId: "",
    materialTypeId: "",
  };
  // 表单配置项
  #formOption: FormOption = {
    formItems: [
      {
        field: "name",
        prop: "name",
        type: "input",
        label: "物料名称",
        placeholder: "请输入物料名称",
        colSpan: 24,
        maxlength: 54,
      },
      {
        field: "materialUnitId",
        prop: "materialUnitId",
        type: "select",
        label: "单位",
        options: [],
        placeholder: "请选择单位",
        colSpan: 24,
      },
      {
        field: "materialTypeId",
        prop: "materialTypeId",
        type: "select",
        label: "物料分类",
        options: [],
        placeholder: "请选择物料分类",
        colSpan: 24,
      },
    ],
    labelWidth: "120px", //标签的长度
  };
  // 表单校验
  #formRules: FormRules = {
    name: [{ required: true, message: "请输入物料名称", trigger: "change" }],
    materialUnitId: [{ required: true, message: "请选择单位", trigger: "change" }],
    materialTypeId: [{ required: true, message: "请选择物料分类", trigger: "change" }],
  };
  get formData() {
    return this.#formData;
  }
  get formRules() {
    return this.#formRules;
  }
  get formOption() {
    return this.#formOption;
  }
  // 下拉框数据处理
  public getFormOption(optionsParams?: any) {
    const params = {
      page: 1,
      limit: 1000,
    };
    const p1 = getMaterialType(params).then((res: any) => {
      if (res) {
        const obj: any = res["materialTypes"];
        const { items } = obj;
        const materialTypeList = items.map((item: Item) => {
          return {
            label: item.name,
            value: item.id,
          };
        });
        const materialTypeId = this.#formOption.formItems.find((item) => item.field === "materialTypeId");
        materialTypeId && (materialTypeId.options = materialTypeList);
      }
    });
    const p2 = getMaterialUnit(params).then((res: any) => {
      if (res) {
        const obj: any = res["materialUnits"];
        const { items } = obj;
        const materialUnitList = items.map((item: Item) => {
          return {
            label: item.name,
            value: item.id,
          };
        });
        const materialUnitId = this.#formOption.formItems.find((item) => item.field === "materialUnitId");
        materialUnitId && (materialUnitId.options = materialUnitList);
      }
    });
    return Promise.all([p1, p2]);
  }
}

// 描述内容
export class FormClassification {
  // 定义变量
  #formData: any = {
    code: "",
    typeOrUnitId: "",
  };
  // 表单配置项
  #formOption: FormOption = {
    formItems: [
      {
        field: "code",
        prop: "code",
        classType: 1,
        type: "input",
        isHidden: true,
        label: "物料编号",
        placeholder: "请输入物料编号",
        colSpan: 24,
        maxlength: 12,
      },
      {
        field: "typeOrUnitId",
        prop: "typeOrUnitId",
        classType: 2,
        slotName: "custom",
        isHidden: true,
        label: "物料分类",
        placeholder: "请选择物料分类",
        colSpan: 24,
        maxlength: 24,
      },
      {
        field: "typeOrUnitId",
        prop: "typeOrUnitId",
        classType: 3,
        slotName: "custom",
        isHidden: true,
        label: "单位名称",
        placeholder: "请选择单位名称",
        colSpan: 24,
        maxlength: 24,
      },
    ],
    labelWidth: "120px", //标签的长度
  };
  // 表单校验
  #formRules: FormRules = {
    typeOrUnitId: [{ required: true, message: "请选择", trigger: "change" }],
    code: [
      { required: true, message: "请输入物料编号", trigger: "change" },
      {
        pattern: /^[^\u4e00-\u9fa5]*$/,
        message: "输入标识符不能带中文",
        trigger: "blur",
      },
    ],
  };
  get formData() {
    return this.#formData;
  }
  get formRules() {
    return this.#formRules;
  }
  get formOption() {
    return this.#formOption;
  }
}
